#include "skybox.h"

#include <lampe/xml/IXMLFactoryEnvironment.h>
#include "irrxml.h"
#include <lampe/xml/IXmlNode.h>
#include <ISceneNodeAnimator.h>
#include <ISceneManager.h>
#include <IAnimatedMeshSceneNode.h>

namespace lampe{
namespace xml{
class SkyboxFactory:public IIrrXmlNode{
public:
	SkyboxFactory(irr::IrrlichtDevice*d):IIrrXmlNode(d){}
	virtual LAMPE_VECTOR_VENDOR<String> getProcessedTags(){
		LAMPE_VECTOR_VENDOR<String> list;
		list.push_back("skybox");
		return list;
	};

	virtual lampe::xml::TagResult tagHandlerBefor(lampe::xml::IXMLFactoryEnvironment*environment,lampe::xml::IXmlNode*node){
		irr::scene::ISceneNode*parent=0;

		if ((node->getParent()) && (node->getParent()->getTag())){
			parent=(irr::scene::ISceneNode*)node->getParent()->getTag();
		}

		if (!getDevice())
			printf("DefaultDevice is NULL\n");

		irr::scene::ISceneNode*n=getDevice()->getSceneManager()->addSkyBoxSceneNode(
			getDevice()->getVideoDriver()->getTexture(node->getAttString("up").c_str()),
		getDevice()->getVideoDriver()->getTexture(node->getAttString("down").c_str()),
		getDevice()->getVideoDriver()->getTexture(node->getAttString("left").c_str()),
		getDevice()->getVideoDriver()->getTexture(node->getAttString("right").c_str()),
		getDevice()->getVideoDriver()->getTexture(node->getAttString("front").c_str()),
					getDevice()->getVideoDriver()->getTexture(node->getAttString("back").c_str()),parent);
		node->setTag(n);
		return lampe::xml::NEXT;
	}
};

}//namespace xml
}//namespace lampe

void regSkybox(lampe::xml::IXMLFactoryEnvironment*environment,irr::IrrlichtDevice*d){
	lampe::xml::IFactoryRecord*h=new lampe::xml::SkyboxFactory(d);
	environment->registerHandler(h);
	h->drop();
}
